import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import { SimpleReport } from 'nc-report'
import {ajax,createPage, getMultiLang} from 'nc-lightapp-front'

export default class Test extends Component {
  constructor (props) {
    super(props)
    this.state = {
        json: {}
      }
    }
  
    componentWillMount() {
          let callback = (json) => {
            this.setState({json})
          }
          getMultiLang({moduleId: 2011, currentLocale: 'zh-CN',domainName: 'erm',callback})
        }	
  disposeSearch(meta, props) {
      let DeptTreeOrgRef = 'nccloud.web.erm.action.ref.sqlbuilder.DeptTreeStopRef';//部门
      // 参照过滤
      let items = meta['light_report'].items;
      items.forEach((item) => {
          let filter = item.referFilterAttr;
          if(filter){
            if(filter[item.attrcode] && filter[item.attrcode].length>0){
              filter[item.attrcode] = filter[item.attrcode].join(",");
            }else{
              filter[item.attrcode] ='';
            }
          }else{
            filter ={};
            filter[item.attrcode] ='';
          }
          if (item.attrcode == 'apply_dept') {
              // 如果user_name的参照需要根据前面选中参照的值进行过滤
              item.queryCondition = ()=>{
                let data = props.search.getSearchValByField('light_report', 'apply_org').value.firstvalue;
                return {pk_org: data,TreeRefActionExt:DeptTreeOrgRef,
                  DataPowerOperationCode: 'fi',
                  isDataPowerEnable: 'Y'}; // 根据apply_org过滤
              }
          }
          if (item.attrcode == 'billmaker') {
            item.queryCondition = () => {
                let apply_org = props.search.getSearchValByField('light_report', 'apply_org').value.firstvalue;
                let apply_dept = props.search.getSearchValByField('light_report', 'apply_dept').value.firstvalue;
                return {dept: apply_dept,pk_org:apply_org,
                  DataPowerOperationCode: 'fi',
                  isDataPowerEnable: 'Y'}; // 根据assume_org过滤
                // return{};
            };
          }
          if (item.attrcode == 'pk_iobsclass') {
            item.queryCondition = ()=>{
                let data = props.search.getSearchValByField('light_report', 'apply_org').value.firstvalue;
                return {pk_org: data,
                        DataPowerOperationCode: 'fi',
                        isDataPowerEnable: 'Y'}; // 根据apply_org过滤
            }
          }
            if (item.attrcode === 'apply_org') {
                item.queryCondition = function () {
                    return {
                        TreeRefActionExt: 'nccloud.web.erm.action.ref.sqlbuilder.OrgTreeRefSqlBuilder',
                        DataPowerOperationCode: 'fi',
                        isDataPowerEnable: 'Y'
                    };
                };
            }
      });
      return meta; // 处理后的过滤参照返回给查询区模板
  }

  /**
    *
    * items: 查询区查询数据，如需拓展查询区参数，请返回与items相同格式的查询数据
  */
  expandSearchVal(items) {
      // 变量赋值拓展
      console.log(items);
      if (items && items.length > 0) {
          items.forEach((item) => {
              if (item.field == 'user_name') {
                  // 需要进行拓展的变量
                  if (item.value.firstvalue == '11') {
                      let obj = {
                          //obj对象内oprtype为between时firstvalue,secondvalue都有值，其他情况只有firstvalue有值
                          field: 'user_other',
                          oprtype: 'like',
                          value: { firstvalue: '111', secondvalue: '222' },
                          notSearch: true //不把此条件放在condition内
                      };
                      items.push(obj);
                  }
              }
          });
      }
      return items;
  }

  /**
    * searchId: 查询区需要的searchId参数
    * 'vname': 需要附默认值的字段
    * {value: '111'}: 显示值，区间为[]，具体可以对照平台查询区修改
    * 'like': 为oprtype字段值
  */
  setDefaultVal(searchId, props) {
      //查询区默认显示字段值
      //已生效
      props.search.setSearchValByField(searchId, 'effectstatus', { value: '1',display: this.state.json['201113SQZX-0001']}, '=');
  }

  /**
     * props: props
     * searchId: 查询区需要的searchId参数
     * field: 编辑后的key
     * val: 编辑后的value
    */
   onAfterEvent (props, searchId, field, val) {
    //查询区编辑后事件
    console.log(props)
    console.log(searchId)
    console.log(field)
    console.log(val)
    }

    /**
     * isRange: 查询后数据区域内点击单元格按钮可操作。如需要像升降序按钮那样，则设置 disabled={isRange}属性
     * data: 表格数据
     * coords: 选中的单元格
    */
    CreateNewSearchArea (isRange, data, coords) {
    // button区域业务端自定义的新增按钮
    //return <NCButton disabled={isRange} className="btn" shape="border">新增按钮</NCButton>
    }

    /**
    * transSaveObject: 业务端处理的transSaveObject参数
    * obj: 点击联查下拉的item信息
    * data: 联查需要的参数
    * props: 平台props
    * url: 平台openTo第一个参数
    * urlParams: 平台openTo第二个参数
    * sessonKey: sessionStorage的key
    */
   setConnectionSearch(transSaveObject, obj, data, props, url, urlParams, sessonKey) {
    if (obj.key == '001'){
        let pkCode = urlParams.reportId;
        let defaultCallBack = (res) => {
          if (res.success) {
            props.openTo(res.data.url, {
              ...res.data,
              status: 'browse'
            });
          }
        };
        ajax({
          url: '/nccloud/erm/expdetail/LinkBillAction.do',
          data: {
            data,
            pkCode
          },
          success: defaultCallBack
        })
      }
    }

    render() {
        return (
            <div className="table">
                <SimpleReport
                    showAdvBtn={true}
                    setDefaultVal={this.setDefaultVal.bind(this)}
                    expandSearchVal={this.expandSearchVal.bind(this)}
                    disposeSearch={this.disposeSearch.bind(this)}
                    onAfterEvent={this.onAfterEvent.bind(this)}
                    CreateNewSearchArea={this.CreateNewSearchArea.bind(this)}
                    setConnectionSearch={this.setConnectionSearch.bind(this)}
                />
            </div>
        );
    }
}

Test = createPage({
    mutiLangCode: '2011'
  })(Test)
ReactDOM.render(<Test />,document.getElementById('app'));